package com.qen.conditions.update;

import com.qen.conditions.AbstractChainWrapper;
import com.qen.repository.BaseDao;
import com.qen.wrapper.update.UpdateWrapper;
import com.qen.wrapper.update.interfaces.UpdateBase;
import org.beetl.sql.clazz.kit.BeetlSQLException;

/**
 * @author licz
 * @data 2025年05月26日 9:53 AM
 */
public class UpdateChainWrapper<T> extends AbstractChainWrapper<T, String, UpdateChainWrapper<T>, UpdateWrapper<T>>
        implements ChainUpdate<T>, UpdateBase<UpdateChainWrapper<T>, String> {

    private final BaseDao<T> baseDao;
    private final Class<T> entityClass;

    public UpdateChainWrapper(BaseDao<T> baseDao) {
        super();
        this.baseDao = baseDao;
        this.entityClass = null;
        super.wrapperChildren = new UpdateWrapper<>();
    }

    public UpdateChainWrapper(Class<T> entityClass) {
        super();
        this.baseDao = null;
        this.entityClass = entityClass;
        super.wrapperChildren = new UpdateWrapper<>();
    }

    @Override
    public UpdateChainWrapper<T> set(boolean condition, String column, Object val, String mapping) {
        wrapperChildren.set(condition, column, val, mapping);
        return typedThis;
    }

    @Override
    public UpdateChainWrapper<T> setSql(boolean condition, String setSql, Object... params) {
        wrapperChildren.setSql(condition, setSql, params);
        return typedThis;
    }

    @Override
    public UpdateChainWrapper<T> setIncrBy(boolean condition, String column, Number val) {
        wrapperChildren.setIncrBy(condition, column, val);
        return typedThis;
    }

    @Override
    public UpdateChainWrapper<T> setDecrBy(boolean condition, String column, Number val) {
        wrapperChildren.setDecrBy(condition, column, val);
        return typedThis;
    }

    @Override
    public String getSqlSet() {
        throw new BeetlSQLException(BeetlSQLException.ERROR, String.format("can not use this method for \"%s\"", "getSqlSet"));
    }

    @Override
    public BaseDao<T> getBaseDao() {
        return baseDao;
    }

    @Override
    public Class<T> getEntityClass() {
        return entityClass;
    }

    public LambdaUpdateChainWrapper<T> lambda() {
        return new LambdaUpdateChainWrapper<>(
                baseDao,
                wrapperChildren.lambda()
        );
    }
}
